<template>
  <div class="app-container" v-loading="loading">
    <div class="action-container">
      <!--<el-button type="primary" icon="el-icon-plus" size="small" @click="openDeskRoomAddDialog">添加位置</el-button>-->
      <el-button type="primary" icon="el-icon-refresh" size="small" @click="refresh">刷新</el-button>
      <el-button icon="el-icon-delete" size="small" v-if="!multipleSelectionMode"
                 @click="multipleSelectionMode=!multipleSelectionMode">批量删除
      </el-button>
      <el-button type="danger" v-if="multipleSelectionMode" icon="el-icon-delete" size="small" @click="deleteDeskRooms">
        删除
      </el-button>
      <el-button v-if="multipleSelectionMode" icon="el-icon-close" size="small" @click="multipleSelectionMode=false">
        取消
      </el-button>
    </div>
    <!--<el-table
      :data="tableData"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="name"
        label="位置名称"
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
      >
      </el-table-column>
      <el-table-column
        prop="updateTime"
        label="更新时间"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        min-width="100"
        align="left">
        <template slot-scope="scope">
          <el-button
            icon="el-icon-edit"
            size="mini"
            type="primary"
            @click="openEditDialog(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>-->
    <!--******************************************************-->
    <!--方块显示餐桌位置-->

    <div class="desk_all_box">
      <el-row>
        <el-col :xs="12" :sm="8" :md="6" :lg="4" :xl="4" class="desk_square">
          <div class="desk_square_content add_desk" @click="openDeskRoomAddDialog">
            <p class="">
              <i></i>
              <span>添加位置</span>
            </p>
          </div>
        </el-col>
        <!--↑是添加事件-->

        <template v-for="room in tableData">
          <el-col :xs="12" :sm="8" :md="6" :lg="4" :xl="4" class="desk_square">
            <div class="desk_square_content">
              <div class="table_num">
                <p style="text-align: center">
                  <span>{{room.name}}</span>
                </p>
              </div>
              <div class="table_type">
                <span @click="openEditDialog(room)"
                      style="position: absolute;top: 50%;font-size: 14px;color: #666666;margin-top: -10px;left: 15px">编辑</span>
                <span @click="deleteRoom(room)"
                      style="position: absolute;top: 50%;font-size: 14px;color: #666666;margin-top: -10px;right: 15px">删除</span>
              </div>
              <p :class="{'no-select':multipleSelectionMode}">
                <span :class="{selectImg:room.active}" @click.stop="pitchon(room,room.id)"></span>
              </p>
            </div>
          </el-col>
        </template>


      </el-row>


    </div>


    <!--↓ 分页和模态框-->
    <pagination
      :total-elements="totalElements"
      :change-callback="getDeskRoomList"
      ref="page"></pagination>
    <el-dialog
      title="添加位置"
      :visible.sync="deskRoomAddDialog"
      :before-close="closeDeskRoomAddDialog">
      <el-form :model="form">

        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDeskRoomAddDialog">取 消</el-button>
        <el-button type="primary" @click="addDeskRoom" :loading="btnLoading">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="编辑位置"
      :visible.sync="editDialog"
      :before-close="closeEditDialog">
      <el-form>
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="currentRow.name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeEditDialog">取 消</el-button>
        <el-button type="primary" @click="editDeskRoom" :loading="btnLoading">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {deskRoomList, deskRoomAdd, deskRoomUpdate, deskRoomLogicalDel} from '../api/desk'
  import pagination from '@/components/pagination/index'
  import Vue from 'vue'

  export default {
    components: {pagination},
    name: 'DeskRoom',
    data() {
      return {
        loading: false,
        btnLoading: false,
        multipleSelection: [], // 选中数据-多选
        multipleSelectionMode: false,//多选模式
        totalElements: 0, // 数据条数
        queryList: {}, // 搜素数据
        tableData: [], // 表格数据
        deskRoomAddDialog: false, // 添加角色模态框
        form: {
          name: '',
          storeId:'',
        }, // 添加位置数据
        formLabelWidth: '120px',
        options: [],
        editDialog: false, // 编辑位置模态框
        currentRow: {
          id: '',
          name: '',
        } // 当前表格对象
      }
    },
    mounted() {
      this.getDeskRoomList()
    },
    methods: {
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      handleDelete(row) {
        this.multipleSelection.push(row)
        this.deleteDeskRooms()
      },
      /*
       * 获取位置列表
       * */
      getDeskRoomList(number, pageSize) {
        this.loading = true
        deskRoomList(number, pageSize, this.queryList).then(response => {
          let data = response.obj
          console.log(data)
          this.totalElements = data.totalElements
          this.tableData = data.content
          setTimeout(() => {
            this.loading = false
          }, 500)
        }).catch(() => {
          this.loading = false
        })
      },

      /*
       * 添加位置
       * */
      /*
       * 打开添加位置模态框
       * */
      openDeskRoomAddDialog() {
        this.deskRoomAddDialog = true
      },
      /*
       * 关闭添加位置模态框
       * */
      closeDeskRoomAddDialog() {
        this.deskRoomAddDialog = false
        this.form = {
          name: '',
        }
      },
      /*
       * 添加位置
       * */
      addDeskRoom() {
        this.loading = true
        this.btnLoading = true
        this.form.storeId=sessionStorage.getItem("storeId")
        deskRoomAdd(this.form).then(response => {
          this.$message({
            message: response.msg,
            type: 'success'
          })
          this.btnLoading = false
          this.closeDeskRoomAddDialog()
          this.$refs.page.refresh()
        }).catch(() => {
          this.btnLoading = false
          this.loading = false
        })
      },
      /*
     * 汇集选中的位置，并修改样式,
     * 取消时候，删除被选中列表
     * */
      pitchon(room, id) {
        if (room.active) {
          Vue.set(room, 'active', false)  //为item添加不存在的属性，需要使用vue提供的Vue.set( object, key, value )方法

          for (let i = 0; i < this.multipleSelection.length; i++) {
            if (this.multipleSelection[i].id === id) {
              this.multipleSelection.splice(i, 1);
            }
          }
          console.log(this.multipleSelection)

        } else {
          Vue.set(room, 'active', true)
          this.multipleSelection.push({id: id});
          console.log(this.multipleSelection)
        }
      },
      /*
      * 删除单个位置
      * */
      deleteRoom(room) {
        // 删除单个位置
        let ids = [];
        ids.push(room.id);
        this.$confirm('是否删除位置','提示',{
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type:'warning'
        }).then(()=>{
          deskRoomLogicalDel(ids).then(response => {
            this.$message({
              message: response.msg,
              type: 'success',
              duration: 1000
            });
            this.$refs.page.refresh()
          }).catch(() => {
            this.loading = false
          })
        })
      },
      /*
      * 删除位置
      * */
      deleteDeskRooms() {
        this.$confirm('是否删除位置？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if (this.multipleSelection.length) {
            this.loading = true
            let ids = []
            this.multipleSelection.forEach((item) => {
              ids.push(item.id)
            })
            deskRoomLogicalDel(ids).then(response => {
              this.$message({
                message: response.msg,
                type: 'success',
                duration: 1000
              })
              this.multipleSelection = []
              this.$refs.page.refresh()
            }).catch(() => {
              this.loading = false
            })
          } else {
            this.$message({
              message: '请选择位置',
              type: 'warning',
              duration: 1000
            })
          }
        })
      },

      /*
       * 编辑位置
       * */
      /*
       * 打开编辑位置模态框
       * */
      openEditDialog(row) {
        this.currentRow.id = row.id
        this.currentRow.name = row.name
        this.editDialog = true
      },
      /*
       * 关闭编辑位置模态框
       * */
      closeEditDialog(row) {
        this.editDialog = false
      },
      /*
       * 编辑位置
       * */
      editDeskRoom() {
        this.loading = true
        this.btnLoading = true
        deskRoomUpdate(this.currentRow).then(response => {
          this.$message({
            message: response.msg,
            type: 'success'
          })
          this.btnLoading = false
          this.closeEditDialog()
          this.$refs.page.refresh()
        }).catch(() => {
          this.btnLoading = false
          this.loading = false
        })
      },

      /*
     * 刷新
     * */
      refresh() {
        this.$refs.page.refresh()
      },
      /*
      * 搜索
      * */
      search() {
        this.$refs.page.search()
      },
    }
  }
</script>

<style scoped>
  body .el-table th.gutter {
    display: table-cell !important;
  }

  /*方块显示*/
  .desk_all_box {
    width: 100%;
    height: 100%;
    overflow: hidden;
    padding: 20px 0 20px 0;
    margin-left: -14px;
    margin-right: -14px;
    /*background-color: #e8e4e5;*/
  }

  .desk_square {
    padding-left: 14px;
    padding-right: 14px;
  }

  .desk_square_content {
    position: relative;
    margin-bottom: 16px;
    height: 98px;
    border-radius: 5px;
    background-color: white;
    border: 1px solid #d0d0d0;
  }

  .desk_square :hover {
    cursor: pointer;
  }

  .add_desk > p {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 84px;
    height: 18px;
    margin-top: -9px;
    margin-left: -42px;
  }


  .add_desk > p span {
    position: relative;
    top: -3px;
    left: 4px;
    font-size: 14px;
    color: #999999;
  }

  .add_desk > p i {
    display: inline-block;
    width: 18px;
    height: 18px;
    margin-top: -9px;
    background: url() no-repeat;
    background-size: 100% 100%;
  }

  .add_desk :hover p i {
    background-size: 100% 100%;
    background: url() no-repeat;
  }

  .table_num {
    width: 100%;
    height: 64px;
    line-height: 64px;
    margin-top: -17px;
    background-color: #626262;
    border-radius: 4px 4px 0 0;
  }

  .table_num span {
    color: white;
    font-size: 20px;
  }

  .table_type {
    position: relative;
    width: 100%;
    height: 34px;
  }

  .no-select {
    position: absolute;
    top: -25px;
    right: -10px;
    width: 26px;
    height: 26px;
  }

  .no-select span {
    display: inline-block;
    width: 26px;
    height: 26px;
    background: url() no-repeat;
    background-size: 100% 100%;
  }

  .selectImg {
    background: url() no-repeat !important;
    background-size: 100% 100%;
  }

</style>
